This is a simple demo to illustrate Feature Extraction and Image classification. While Batman vs Superman is yet to kick off, here is my take on the problem.
#necessary imports
from skimage import io
from skimage.color import rgb2gray, rgb2hsv
from skimage.util import img_as_ubyte, img_as_int
import os
import numpy as np
import warnings
from matplotlib import pyplot as plt
warnings.filterwarnings("ignore")
from sklearn.neighbors import KNeighborsClassifier
%pylab inline
#custom function to diplsya images, handlers grayscale and size is bigger
def show_img(img):
width = 10
height = img.shape[0]*float(width)/img.shape[1]
plt.figure(figsize = (width,height))
ax = plt.imshow(img, cmap='gray',aspect='auto')
#Calculates, average grayscale , if beguf is True, displays intermediate image
def mean_gray(img, debug = False):
img = rgb2gray(img)
img = img_as_ubyte(img)
if debug:
show_img(img)
return np.mean(img)
img = io.imread('images/1.jpg')
print mean_gray(img, True)
img = io.imread('images/2.jpg')
print mean_gray(img, True)
#converts image to HSV, to compare with blue color
def mean_blue_diff(img, debug = False):
img = rgb2hsv(img)
img = img_as_ubyte(img)
hue = img[:,:,0]
diff = hue - 140 # hue value of blue
# pixels within a threshold of blue color
diff = np.abs(diff) < 10
if debug :
show_img(diff)
return np.mean(diff*255)
img = imread('images/6.jpg')
print mean_blue_diff(img, True)
img = imread('images/2.jpg')
print mean_blue_diff(img, True)
images = [ io.imread('images/' + name) for name in sort(os.listdir('images')) ]
training_data = [ (mean_gray(i),mean_blue_diff(i)) for i in images ]
training_data = np.array(training_data)
#plt.scatter(training_data[:,0], training_data[:,1])
expected_values = [1,0,0,0,0,1,1,1] # superman=1, batman=0 as class labels
for i in range(len(training_data)):
a,b = training_data[i]
if expected_values[i] == 0:
plt.scatter( [a], [b] , color='#FF0000')
else :
plt.scatter( [a], [b] , color='#0000FF')
neighbours = KNeighborsClassifier()
neighbours.fit(training_data, expected_values)
batman_logo = imread('logos/batman.jpg')
brows,bcols = batman_logo.shape[0:2]
superman_logo = imread('logos/superman.jpg')
srows,scols = superman_logo.shape[0:2]
batman_points = []
superman_points = []
for image in images:
a,b = mean_gray(image), mean_blue_diff(image)
prediction = neighbours.predict((a, b))
if prediction[0] == 1:
superman_points += [(a,b)]
image[:srows,:scols] = superman_logo
else:
batman_points += [(a,b)]
image[:brows, :bcols] = batman_logo
show_img(image)
final_img = np.zeros((255,255,3),dtype = np.uint8)
for i in range(255):
for j in range(255):
prediction = neighbours.predict((i,j))[0]
if prediction == 0 :
final_img[254 - j,i] = 0,0,0
else:
final_img[254 - j,i] = 0,0,255
show_img(final_img)